# -*- coding: UTF-8 -*-
import urllib.request
import pandas as pd
import time,datetime,re
pd.set_option('display.expand_frame_repr', False)
from loguru import logger
from colorama import init, Fore, Back, Style
init(autoreset=True)
def get_data(ts_code,scale,datalen,encoding="gbk",):
    # 在地址里symbol指的是股票代码，这里需要注意的是不能只填数字代码，还需要把交易市场的前缀加上去，比如sz000001指的是平安银行，
    # 而sh000001则是上证指数；
    # scale表示的是时间长度，以分钟为基本单位，输入240就表示下载日K线数据，
    # 60就是小时K线数据，貌似最短时间是5分钟，并没有提供分钟数据；
    # datalen则是获取数据的条数，在日K线的时间长度了，
    # datalen就是获取60天日K数据，当然也可以获取60小时K数据。
    # sinaStockData((sinaStockUrl(240)))
    url = "https://money.finance.sina.com.cn/quotes_service/api/jsonp_v2.php/var=/CN_MarketData.getKLineData?"
    url += "symbol="+ts_code+"&scale="+str(scale)+"&ma=no&datalen="+str(datalen)
    # print(url)
    page = urllib.request.urlopen(url)
    html = page.read()
    html = html.decode(encoding)
    # print("原始字符","="*50)
    # print(html)
    tmpHtml = html[html.index("var=(") + len("var=("):]
    final_html = tmpHtml[:len(tmpHtml) - 2]
    final_html = eval(final_html)
    # print("原始字符",type(final_html))
    # print(final_html)
    df = pd.DataFrame(final_html)

    period="日线"
    if scale==240:
        period = "日线"
    if scale==5:
        period = "5MIN"
    if scale==15:
        period = "15MIN"
    if scale==30:
        period = "30MIN"
    if scale==60:
        period = "60MIN"

    # df['adj'] = "不复权"
    # df['TAG'] = "原始数据"
    # df['SOURCE'] = "新浪数据"
    df.insert(0,"ts_code",ts_code,allow_duplicates=False)
    df.insert(1, "trade_date", df.day.astype('str').str[0:10], allow_duplicates=False)
    df.insert(2, "trade_time", df.day.astype('str').str[10:len(df.day)], allow_duplicates=False)
    df['period'] = period
    df['adj'] = "不复权"
    df['TAG'] = "原始数据"
    df['SOURCE'] = "新浪数据"
    # df['trade-date'] = df.day.astype('str').str[0:10]
    # df['trade-time'] = df.day.astype('str').str[10:len(df.day)]
    # df.insert(df.shape[0], "adj", ts_code, allow_duplicates=False)
    # df.drop([len(df) - 1], inplace=True)
    # df.columns = df.columns.str.upper()
    df.drop(['day'], axis=1,inplace=True)
    df.columns = df.columns.str.upper()
    return df

def init_codelist():
    import tushare as ts
    ts.set_token('531f7eda35d265a43c7091a5971802aab320a80c16afb6e7164c595a')
    pro = ts.pro_api()
    df = pro.stock_basic(exchange='', list_status='L', fields='ts_code,symbol,name,area,industry,list_date')
    df.to_csv("code_list.csv")
def exe(M,N):
    logger.info("exec()...")
    df = pd.read_csv("code_list.csv")
    code_lis = df["ts_code"].values.tolist()
    # print(len(code_lis),code_lis)
    i=0
    for __list in code_lis:
        codetmpe=__list[0:6]
        mk=__list[-2:]
        tscode=str(mk+codetmpe).lower()
        # print(__list,mk,codetmpe,tscode)

        starttime = datetime.datetime.now()

        # if M==5:
        #     df = get_data(tscode,M,N)
        #     df.to_csv("C://sinaData//" + tscode + ".M5.CSV")
        #     # print(df)
        if M==15:
            df = get_data(tscode,M,N)
            df.to_csv("C://sinaData//15m//" + tscode + ".M15.CSV")
            # print(df)
        # if M == 30:
        #     df = get_data(tscode, M, N)
        #     df.to_csv("C://sinaData//" + tscode + ".M30.CSV")
        #     # print(df)
        # if M == 240:
        #     df = get_data(tscode, M, N)
        #     df.to_csv("C://sinaData//" + tscode + ".DAY.CSV")
            # print(df)
        # df.to_csv("C://sinaData//"+tscode+".DAY.CSV")

        endtime = datetime.datetime.now()
        print(i,"code:"+tscode+"\tdf size:"+str(df.shape[0]),"get time：" + str(endtime - starttime))
        i+=1
        # if i==1:
        #     break

if __name__ == '__main__':
    # print("program is running....")
    # init_codelist()
    logger.success("程序开始运行。。。")
    # 统计程序开始时间
    starttime = datetime.datetime.now()

    # exe(5, 1000)
    # # time.sleep(5000)
    exe(15,1000)
    # time.sleep(5000)
    # exe(30, 1000)
    # time.sleep(5000)
    # exe(240, 1000)
    # print("df=================================================================df=================================================================df")
    # df = get_data("sz000518",5,2000)
    # print(df)
    # print("df=================================================================df=================================================================df")
    # 统计程序开结束时间
    endtime = datetime.datetime.now()
    logger.success("程序运行时间：" + str(endtime - starttime))
    logger.success('=============================\t程序运行结束\t=============================')
